package org.viva.service.cms;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.viva.admin.util.page.Page;
import org.viva.core.dao.BaseDAO;
import org.viva.entity.cms.Content;

@Service("contentService")
public class ContentService {

    @Resource
    private BaseDAO<Content> dao;

    @SuppressWarnings("unchecked")

    public List<Content> getList(Map<String, Object> param) {
        Criteria criteria = dao.getCurrentSession().createCriteria(Content.class);
        criteria.add(Restrictions.eq("cgId", Long.parseLong(param.get("cgid") + "")));
        return criteria.list();
    }

    public Content get(Long id) {
        return dao.get(Content.class, id);
    }

    public Page getPage(Map<String, Object> param) {
        Page page = new Page(param);
        Criteria criteria = dao.getCurrentSession().createCriteria(Content.class);
        if (param.get("cgid") != null) {
            criteria.add(Restrictions.eq("cgId", Long.parseLong(param.get("cgid") + "")));
        }
        if (param.get("status") != null) {
            criteria.add(Restrictions.eq("status", param.get("status") + ""));
        }
        if (param.get("tag") != null && !"".equals(param.get("tag"))) {
            criteria.add(Restrictions.like("tags", param.get("tag") + "", MatchMode.ANYWHERE));
        }
        // 获取根据条件分页查询的总行数
        int rowCount = Integer.parseInt(criteria.setProjection(Projections.rowCount()).uniqueResult() + "");
        if (param.get("ord") != null && !"".equals(param.get("ord") + "")) {
            if ("desc".equals(param.get("ordtype") + "")) {
                criteria.addOrder(Order.desc(param.get("ord") + ""));
            } else {
                criteria.addOrder(Order.asc(param.get("ord") + ""));
            }
        }
        criteria.setProjection(null);
        criteria.addOrder(Order.desc("id"));
        criteria.setFirstResult((page.getPage() - 1) * page.getRows());
        criteria.setMaxResults(page.getRows());
        page.addDate(criteria.list(), rowCount);
        return page;
    }

    public void up(Content content) {
        Content c = dao.get(Content.class, content.getId());
        c.setTitle(content.getTitle());
        c.setContent(content.getContent());
        c.setCreateDate(content.getCreateDate());
        c.setTags(content.getTags());
        c.setCgId(content.getCgId());
        c.setUserId(content.getUserId());
        c.setImg(content.getImg());
        initContent(c);
    }

    public void add(Content content) {
        content.setCommCount(0L);
        content.setViewCount(0L);
        initContent(content);
        dao.saveOrUpdate(content);
    }

    private void initContent(Content c) {
        Document doc = Jsoup.parse(c.getContent());
        if (StringUtils.isEmpty(c.getIntro())) {
            String A_INTRO = doc.text();
            // 简介
            A_INTRO = A_INTRO.replaceAll("[\r\n]", "");
            A_INTRO = A_INTRO.substring(0, A_INTRO.length() > 300 ? 300 : A_INTRO.length());
            c.setIntro(A_INTRO);
        }
        if (StringUtils.isEmpty(c.getImg())) {
            Elements elements = doc.select("img");
            if (elements.size() > 0) {
                String img = elements.get(0).attr("src");
                c.setImg(img);
            }
        }
    }

    public void del(Long id) {
        if (id == null) return;
        Content content = dao.get(Content.class, id);
        if (content == null) return;
        dao.delete(content);
    }

}
